package producer.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import producer.constant.BrokerMessageStatus;
import producer.entity.BrokerMessage;
import producer.mapper.BrokerMessageMapper;

import javax.annotation.Resource;
import java.util.Date;

/**
 * Created with IDEA
 * Author: LuoHengYi
 * Date: 2022/6/18
 * Time: 21:00
 */
@Service
public class MessageStoreService {


    @Resource
    private BrokerMessageMapper brokerMessageMapper;


    public int insert(BrokerMessage brokerMessage) {

        return brokerMessageMapper.insert(brokerMessage);
    }


    /**
     * 消息发送成功修改数据库状态
     * @param messageId  messageId
     */
    public void success(String messageId) {
        brokerMessageMapper.changeBrokerMessageStatus(
                messageId, BrokerMessageStatus.SEND_OK.getCode(),new Date()
        );
    }

    /**
     * 消息发送失败修改数据库状态
     * @param messageId  messageId
     */
    public void failure(String messageId) {
        brokerMessageMapper.changeBrokerMessageStatus(
                messageId, BrokerMessageStatus.SEND_FAIL.getCode(),new Date()
        );
    }
}
